#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void StackDestroy(Stack* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void CheckCapacity(Stack* ps)
{
	int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
	Stack* tmp = (Stack*)realloc(ps->a,sizeof(Stack*)* newcapacity);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->a = tmp;
	ps->capacity = newcapacity;

}

void StackPush(Stack* ps, STDateType data)
{
	CheckCapacity(ps);
	ps->a[ps->top] = data;
	ps->top++;
}

bool StackEmpty(Stack* ps)
{
	return ps->top == 0;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

STDateType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}